﻿#include "colwidget.h"
#include "ui_colwidget.h"

#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include <QPainter>
#include <QTextDocument>
#include <QTextBlock>

int COLwidget::identification=0;
COLwidget::COLwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::COLwidget)
{
    ui->setupUi(this);
    LendOrConsult();
}

COLwidget::~COLwidget()
{
    delete ui;}


void COLwidget::LendOrConsult()
{
    ui->lende->setDate(QDate::currentDate());
    ui->returne->setDate(QDate::currentDate());
    ui->lends->setDate(QDate::currentDate());
    ui->returns->setDate(QDate::currentDate());
    ui->consulttw->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->consulttw->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->consulttw->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->cobjectmentw->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->cobjectmentw->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->clookmentw->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->clookmentw->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->cobjectmentw->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->clookmentw->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->consult->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->consult->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->consult->setSelectionMode(QAbstractItemView::SingleSelection);
    //设置各种表单行选中且不能编辑
    ui->lendrbtn->setChecked(true);
    ui->consult->horizontalHeader()->setResizeMode(QHeaderView::Stretch); //自适应列宽
    ui->consulttw->horizontalHeader()->setResizeMode(QHeaderView::Stretch); //自适应列宽
    ui->cobjectmentw->horizontalHeader()->setResizeMode(QHeaderView::Stretch); //自适应列宽
    ui->clookmentw->horizontalHeader()->setResizeMode(QHeaderView::Stretch); //自适应列宽
    ui->consult->hideColumn(0);
    ui->cobjectmentw->hideColumn(0);
    ui->cobjectmentw->hideColumn(6);
    ui->clookmentw->hideColumn(0);
    ui->clookmentw->hideColumn(5);
    ui->consulttw->hideColumn(0);
    ui->consulttw->hideColumn(9);
    //设置初始状态并隐藏相关的列
    connect(ui->consult->horizontalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(COLshowsort(int)));
    QSqlQuery query;
    QString yuju=QString("select ApprovalID,lendway,lenddate,returndate,recordman,others from ZZB.Approval where distinction='%1' limit 20").arg(0);
    query.exec(yuju);
    while(query.next())
    {
        ui->consult->insertRow(0);
        QString a=query.value(0).toString();
        ui->consult->setItem(0,0,new QTableWidgetItem(a));

        QString b=query.value(1).toString();
        ui->consult->setItem(0,1,new QTableWidgetItem(b));

        QString c=query.value(2).toString();
        ui->consult->setItem(0,2,new QTableWidgetItem(c));

        QString d=query.value(3).toString();
        ui->consult->setItem(0,3,new QTableWidgetItem(d));

        QString e=query.value(4).toString();
        ui->consult->setItem(0,4,new QTableWidgetItem(e));

        QString f=query.value(5).toString();
        ui->consult->setItem(0,5,new QTableWidgetItem(f));
    }

}

void COLwidget::COLclear()
{
    ui->consult->clearContents();
    ui->cobjectmentw->clearContents();
    ui->clookmentw->clearContents();
    ui->consult->setRowCount(0);
    ui->cobjectmentw->setRowCount(0);
    ui->clookmentw->setRowCount(0);
    ui->cconsultcontent->clear();
    ui->cconsultreason->clear();
    ui->cinquerydate->clear();
    ui->cinquerylead->clear();
    ui->cinqueryopinion->clear();
    ui->cofficedate->clear();
    ui->cofficelead->clear();
    ui->cofficeopinion->clear();
    ui->cunitdate->clear();
    ui->cunitlead->clear();
    ui->cunitopinion->clear();
    ui->consulttw->setRowCount(0);
}

void COLwidget::COLshowrelate()
{
    int i=ui->consult->item(ui->consult->currentRow(),0)->text().toInt();
    QString judge=QString("select reason,content,inquerylead,inquerydate,inqueryopinion,manageofficelead,manageofficedate,manageofficeopinion,manageunitlead,manageunitdate,manageunitopinion from ZZB.Approval where ApprovalID='%1'").arg(i);
       QSqlQuery query;
       query.exec(judge);
       while(query.next())
       {
           QString a=query.value(0).toString();
           ui->cconsultreason->setText(a);

           QString b=query.value(1).toString();
           ui->cconsultcontent->setText(b);

           QString c=query.value(2).toString();
           ui->cinquerylead->setText(c);

           QString d=query.value(3).toString();
           ui->cinquerydate->setText(d);

           QString e=query.value(4).toString();
           ui->cinqueryopinion->setText(e);

           QString f=query.value(5).toString();
           ui->cofficelead->setText(f);

           QString g=query.value(6).toString();
           ui->cofficedate->setText(g);

           QString h=query.value(7).toString();
           ui->cofficeopinion->setText(h);

           QString i=query.value(8).toString();
           ui->cunitlead->setText(i);

           QString j=query.value(9).toString();
           ui->cunitdate->setText(j);

           QString k=query.value(10).toString();
           ui->cunitopinion->setText(k);
       }
}

void COLwidget::COLshowobject()
{
    int i=ui->consult->item(ui->consult->currentRow(),0)->text().toInt();
    ui->cobjectmentw->clearContents();
    ui->cobjectmentw->setRowCount(0);
    QString judge=QString("select *from ZZB.Object where ApprovalID='%1'").arg(i);
    QSqlQuery query;
    query.exec(judge);
    while(query.next())
    {
        ui->cobjectmentw->insertRow(0);
        QString z=query.value(0).toString();
        ui->cobjectmentw->setItem(0,0,new QTableWidgetItem(z));

        QString m=query.value(1).toString();
        ui->cobjectmentw->setItem(0,1,new QTableWidgetItem(m));

        QString n=query.value(2).toString();
        ui->cobjectmentw->setItem(0,2,new QTableWidgetItem(n));

        QString b=query.value(3).toString();
        ui->cobjectmentw->setItem(0,3,new QTableWidgetItem(b));

        QString v=query.value(4).toString();
        ui->cobjectmentw->setItem(0,4,new QTableWidgetItem(v));

        QString c=query.value(5).toString();
        ui->cobjectmentw->setItem(0,5,new QTableWidgetItem(c));

        QString x=query.value(6).toString();
        ui->cobjectmentw->setItem(0,6,new QTableWidgetItem(x));
    }
}

void COLwidget::COLshowlook()
{
    int i=ui->consult->item(ui->consult->currentRow(),0)->text().toInt();
    ui->clookmentw->clearContents();
    ui->clookmentw->setRowCount(0);
    QString judge=QString("select *from ZZB.ReadMen where ApprovalID='%1'").arg(i);
       QSqlQuery query;
       query.exec(judge);
       while(query.next())
       {
           ui->clookmentw->insertRow(0);
           QString z=query.value(0).toString();
           ui->clookmentw->setItem(0,0,new QTableWidgetItem(z));

           QString m=query.value(1).toString();
           ui->clookmentw->setItem(0,1,new QTableWidgetItem(m));

           QString n=query.value(2).toString();
           ui->clookmentw->setItem(0,2,new QTableWidgetItem(n));

           QString b=query.value(3).toString();
           ui->clookmentw->setItem(0,3,new QTableWidgetItem(b));

           QString v=query.value(4).toString();
           ui->clookmentw->setItem(0,4,new QTableWidgetItem(v));

           QString c=query.value(5).toString();
           ui->clookmentw->setItem(0,5,new QTableWidgetItem(c));
       }
}

void COLwidget::COLshowrecord()
{
    int i=ui->consult->item(ui->consult->currentRow(),0)->text().toInt();
    ui->consulttw->clearContents();
    ui->consulttw->setRowCount(0);
    QString judge=QString("select *from ZZB.recordall where ApprovalID='%1'").arg(i);
    QSqlQuery query;
    query.exec(judge);
    while(query.next())
    {
        ui->consulttw->insertRow(0);
        QString z=query.value(0).toString();
        ui->consulttw->setItem(0,0,new QTableWidgetItem(z));

        QString m=query.value(1).toString();
        ui->consulttw->setItem(0,1,new QTableWidgetItem(m));

        QString n=query.value(2).toString();
        ui->consulttw->setItem(0,2,new QTableWidgetItem(n));
        if(QString::compare(ui->consult->item(ui->consult->currentRow(),3)->text(),n)<0){
            ui->consulttw->item(0,2)->setForeground(QBrush(QColor(255,0,0)));
        }

        QString b=query.value(3).toString();
        ui->consulttw->setItem(0,3,new QTableWidgetItem(b));

        QString v=query.value(4).toString();
        ui->consulttw->setItem(0,4,new QTableWidgetItem(v));

        QString c=query.value(5).toString();
        ui->consulttw->setItem(0,5,new QTableWidgetItem(c));

        QString x=query.value(6).toString();
        ui->consulttw->setItem(0,6,new QTableWidgetItem(x));

        QString p=query.value(7).toString();
        ui->consulttw->setItem(0,7,new QTableWidgetItem(p));

        QString g=query.value(8).toString();
        ui->consulttw->setItem(0,8,new QTableWidgetItem(g));

        QString o=query.value(9).toString();
        ui->consulttw->setItem(0,9,new QTableWidgetItem(o));
    }

}

void COLwidget::on_lendrbtn_clicked()
{
    COLclear();
    ui->consulttab->setCurrentIndex(0);
    QSqlQuery query;
    QString yuju=QString("select ApprovalID,lendway,lenddate,returndate,recordman,others from ZZB.Approval where distinction='%1'").arg(0);
    query.exec(yuju);
    while(query.next())
    {
        ui->consult->insertRow(0);
        QString a=query.value(0).toString();
        ui->consult->setItem(0,0,new QTableWidgetItem(a));

        QString b=query.value(1).toString();
        ui->consult->setItem(0,1,new QTableWidgetItem(b));

        QString c=query.value(2).toString();
        ui->consult->setItem(0,2,new QTableWidgetItem(c));

        QString d=query.value(3).toString();
        ui->consult->setItem(0,3,new QTableWidgetItem(d));

        QString e=query.value(4).toString();
        ui->consult->setItem(0,4,new QTableWidgetItem(e));

        QString f=query.value(5).toString();
        ui->consult->setItem(0,5,new QTableWidgetItem(f));
    }
}

void COLwidget::on_consultrbtn_clicked()
{
    COLclear();
    ui->consulttab->setCurrentIndex(0);
    QSqlQuery query;
    QString yuju=QString("select ApprovalID,lendway,lenddate,returndate,recordman,others from ZZB.Approval where distinction='%1'").arg(1);
    query.exec(yuju);
    while(query.next())
    {
        ui->consult->insertRow(0);
        QString a=query.value(0).toString();
        ui->consult->setItem(0,0,new QTableWidgetItem(a));

        QString b=query.value(1).toString();
        ui->consult->setItem(0,1,new QTableWidgetItem(b));

        QString c=query.value(2).toString();
        ui->consult->setItem(0,2,new QTableWidgetItem(c));

        QString d=query.value(3).toString();
        ui->consult->setItem(0,3,new QTableWidgetItem(d));

        QString e=query.value(4).toString();
        ui->consult->setItem(0,4,new QTableWidgetItem(e));

        QString f=query.value(5).toString();
        ui->consult->setItem(0,5,new QTableWidgetItem(f));
    }
}

void COLwidget::on_addbtn_clicked()
{
    int i=ui->consult->rowCount();
    COLaddApprovalDialog coladdapp(this);
    connect(this,SIGNAL(sendDa(int)),&coladdapp,SLOT(receivedistinction(int)));
    bool ok=ui->lendrbtn->isChecked();
    if(ok){
    emit sendDa(0);
    }else{emit sendDa(1);}
    coladdapp.exec();
    if(ui->consult->rowCount()==(i+1)){
    if(ui->lendrbtn->isChecked()){on_lendrbtn_clicked();}
    else{on_consultrbtn_clicked();}
    ui->consult->selectRow(0);}
}

void COLwidget::on_lendstart_clicked()
{
    bool ok;
    ok=ui->lends->isEnabled();
    if(ok){
    ui->lends->setEnabled(false);
    }
    else
    {
    ui->lends->setEnabled(true);
    }
}

void COLwidget::on_lendend_clicked()
{
    bool ok;
    ok=ui->lende->isEnabled();
    if(ok){
    ui->lende->setEnabled(false);
    }
    else
    {
    ui->lende->setEnabled(true);
    }
}

void COLwidget::on_returnstart_clicked()
{
    bool ok;
    ok=ui->returns->isEnabled();
    if(ok){
    ui->returns->setEnabled(false);
    }
    else
    {
    ui->returns->setEnabled(true);
    }
}

void COLwidget::on_returnend_clicked()
{
    bool ok;
    ok=ui->returne->isEnabled();
    if(ok){
    ui->returne->setEnabled(false);
    }
    else
    {
    ui->returne->setEnabled(true);
    }
}

void COLwidget::on_deletebtn_clicked()
{
    if(ui->consult->currentRow()>-1)
    {
    QString ID=ui->consult->item(ui->consult->currentRow(),0)->text();
    QSqlQuery query;
    QString strsql=QString("DELETE FROM Approval WHERE ApprovalID='%1'").arg(ID);
    if(query.exec(strsql)) {QMessageBox::information(this,"提示信息","删除成功!");
    on_seekbtn_clicked();}
    else {QMessageBox::information(this,"提示信息","删除失败!");}}
    else{QMessageBox::information(this, tr("提示信息"), tr("无法删除：未选中数据！"));}
}

void COLwidget::on_editbtn_clicked()
{
    int i=ui->consult->currentRow();
    if(i>-1){
    COLeditappDialog coleditapp(this);
    connect(this,SIGNAL(sendData(QStringList)),&coleditapp,SLOT(receivestr(QStringList)));
    QStringList fan;
    fan<<ui->consult->item(i,0)->text()<<ui->consult->item(i,1)->text()<<ui->consult->item(i,2)->text()<<ui->consult->item(i,3)->text()<<ui->consult->item(i,4)->text()<<ui->consult->item(i,5)->text()<<ui->cconsultreason->text()<<ui->cconsultcontent->text()<<ui->cinquerylead->text()<<ui->cinquerydate->text()<<ui->cinqueryopinion->text()<<ui->cofficelead->text()<<ui->cofficedate->text()<<ui->cofficeopinion->text()<<ui->cunitlead->text()<<ui->cunitdate->text()<<ui->cunitopinion->text();
    emit sendData(fan);
    coleditapp.exec();
    on_seekbtn_clicked();}
    else{QMessageBox::information(this,"提示信息","请选中编辑行!");}
}

void COLwidget::on_consult_cellClicked(int row, int column)
{
    if(row>-1&&column>-1){
        COLshowlook();
        COLshowobject();
        COLshowrecord();
        COLshowrelate();
    }
}

void COLwidget::on_seekbtn_clicked()
{
    int select;
    if(ui->consult->currentRow()>-1) select=ui->consult->item(ui->consult->currentRow(),0)->text().toInt();
    QString objectunitstr;
    QString objectstr;
    QString readunitstr;
    QString readstr;
    QString disstr;
    QString lendstart;
    QString lendend;
    QString returnstart;
    QString returnend;
    QString str;
    QSqlQuery query;

    if(ui->lendrbtn->isChecked()) { disstr=QString(" a.distinction=0");}
    else{disstr=QString(" a.distinction=1");}
    bool objnameok=!ui->objectname->text().isEmpty();
    bool objunitok=!ui->objunit->text().isEmpty();
    bool readmanok=!ui->consultman->text().isEmpty();
    bool readunitok=!ui->consultunit->text().isEmpty();
    bool ls=ui->lendstart->isChecked();
    bool le=ui->lendend->isChecked();
    bool rs=ui->returnstart->isChecked();
    bool re=ui->returnend->isChecked();
    if (objunitok) {
        objectunitstr=QString(" AND o.Objectunit='%1'").arg(ui->objunit->text());}
    if (objnameok) {
        objectstr=QString(" AND o.Objectman='%1'").arg(ui->objectname->text());}
    if (readmanok) {
        readstr=QString(" AND r.Readman='%1'").arg(ui->consultman->text());}
    if (readunitok) {
        readunitstr=QString(" AND r.Readunit='%1'").arg(ui->consultunit->text());}
    if(ls){
        lendstart=QString(" AND a.lenddate>='%1'").arg(ui->lends->text());}
    if(le){
        lendend=QString(" AND a.lenddate<='%1'").arg(ui->lende->text());}
    if(rs){
        returnstart=QString(" AND a.returndate>='%1'").arg(ui->returns->text());}
    if(re){
        returnend=QString(" AND a.returndate<='%1'").arg(ui->returne->text());}
    if((objunitok||objnameok)&&(readmanok||readunitok)){
    str=QString("select distinct a.ApprovalID,a.lendway,a.lenddate,a.returndate,a.recordman,a.others from Approval a,ReadMen r,Object o where a.ApprovalID=o.ApprovalID and r.ApprovalID=o.ApprovalID and r.ApprovalID=a.ApprovalID AND"+disstr+objectunitstr+objectstr+readunitstr+readstr+lendstart+lendend+returnstart+returnend);}
    else if(objunitok||objnameok){
    str=QString("select distinct a.ApprovalID,a.lendway,a.lenddate,a.returndate,a.recordman,a.others from Approval a,Object o where a.ApprovalID=o.ApprovalID AND"+disstr+objectunitstr+objectstr+lendstart+lendend+returnstart+returnend);
    }else if(readmanok||readunitok){
    str=QString("select distinct a.ApprovalID,a.lendway,a.lenddate,a.returndate,a.recordman,a.others from Approval a,ReadMen r where r.ApprovalID=a.ApprovalID AND"+disstr+readunitstr+readstr+lendstart+lendend+returnstart+returnend);
    }else{
    str=QString("select distinct a.ApprovalID,a.lendway,a.lenddate,a.returndate,a.recordman,a.others from Approval a WHERE "+disstr+lendstart+lendend+returnstart+returnend);
    }
    ui->consult->clearContents();
    ui->consulttw->clearContents();
    ui->consult->setRowCount(0);
    ui->cobjectmentw->setRowCount(0);
    ui->clookmentw->setRowCount(0);
    ui->cunitdate->clear();
    ui->cunitlead->clear();
    ui->cunitopinion->clear();
    ui->cconsultcontent->clear();
    ui->cconsultreason->clear();
    ui->cofficedate->clear();
    ui->cofficelead->clear();
    ui->cofficeopinion->clear();
    ui->cinqueryopinion->clear();
    ui->cinquerylead->clear();
    ui->cinquerydate->clear();
    query.exec(str);
    while(query.next())
    {
        ui->consult->insertRow(0);
        QString a=query.value(0).toString();
        ui->consult->setItem(0,0,new QTableWidgetItem(a));

        QString b=query.value(1).toString();
        ui->consult->setItem(0,1,new QTableWidgetItem(b));

        QString c=query.value(2).toString();
        ui->consult->setItem(0,2,new QTableWidgetItem(c));

        QString d=query.value(3).toString();
        ui->consult->setItem(0,3,new QTableWidgetItem(d));

        QString e=query.value(4).toString();
        ui->consult->setItem(0,4,new QTableWidgetItem(e));

        QString f=query.value(5).toString();
        ui->consult->setItem(0,5,new QTableWidgetItem(f));
    }
    if(select>0){
        for(int j=0;j<ui->consult->rowCount();j++)
            {
           if(ui->consult->item(j,0)->text().toInt()==select){
               ui->consult->selectRow(j);
               COLshowlook();
               COLshowobject();
               COLshowrecord();
               COLshowrelate();
           //为了显示材料内容表中的函数
           }
        }
    }//编辑之后，调用的是查找函数，在这里默认选中编辑行并显示相关的材料内容
}

void COLwidget::on_addcobject_clicked()
{
    int i;
    int j=ui->cobjectmentw->rowCount();
    int row=ui->consult->currentRow();
    if(row<=-1){QMessageBox::information(this,"提示信息","请选择对应的审批表之后进行添加对象!");
    }else{
        COLaddobjectDialog coladdobj(this);
        connect(this,SIGNAL(sendDa(int)),&coladdobj,SLOT(receiveApprovalID(int)));
    if(ui->cobjectmentw->rowCount()>=3){QMessageBox::information(this,"提示信息","阅档对象人数一般是三人及以下！");}
    i=ui->consult->item(row,0)->text().toInt();
    emit sendDa(i);
    coladdobj.exec();
    if(ui->cobjectmentw->rowCount()==(j+1))COLshowobject();
    }
}

void COLwidget::on_deletecobject_clicked()
{
    if(ui->consult->currentRow()>-1)
    {
    if(ui->cobjectmentw->currentRow()>-1){
    QString ID=ui->cobjectmentw->item(ui->cobjectmentw->currentRow(),0)->text();
    QSqlQuery query;
    QString strsql=QString("DELETE FROM Object WHERE ObjectID='%1'").arg(ID);
    if(query.exec(strsql)) {QMessageBox::information(this,"提示信息","删除成功!");
    COLshowobject();}
    else {QMessageBox::information(this,"提示信息","删除失败!");}
    }else{QMessageBox::information(this, tr("提示信息"), tr("无法删除：未选中删除行！"));}
    }else{QMessageBox::information(this,"提示信息","请选择对应的审批表信息!");}
}

void COLwidget::on_editcobjectbtn_clicked()
{
    int j=ui->cobjectmentw->currentRow();
    int row=ui->consult->currentRow();
    if(row<=-1){QMessageBox::information(this,"提示信息","请选择对应的审批表信息!");
    }else{
        if(j>-1){
        COLeditobjectDialog coleditobj(this);
        connect(this,SIGNAL(sendData(QStringList)),&coleditobj,SLOT(receivestr(QStringList)));
        QStringList fan;
        fan<<ui->cobjectmentw->item(j,0)->text()<<ui->cobjectmentw->item(j,1)->text()<<ui->cobjectmentw->item(j,2)->text()<<ui->cobjectmentw->item(j,3)->text()<<ui->cobjectmentw->item(j,4)->text()<<ui->cobjectmentw->item(j,5)->text();
    emit sendData(fan);
    coleditobj.exec();
    COLshowobject();
    ui->cobjectmentw->selectRow(j);}
    else{QMessageBox::information(this,"提示信息","请选择要编辑的行!");}
    }
}

void COLwidget::on_addclook_clicked()
{
    int i;
    int j=ui->clookmentw->rowCount();
    int row=ui->consult->currentRow();
    if(row<=-1){QMessageBox::information(this,"提示信息","请选择对应的审批表之后进行添加阅档人员!");
    }else{
        COLaddlookDialog coladdlook(this);
        connect(this,SIGNAL(sendDa(int)),&coladdlook,SLOT(receiveApprovalID(int)));
    if(ui->clookmentw->rowCount()>=3){QMessageBox::information(this,"提示信息","阅档对象人数一般是三人及以下！");}
    i=ui->consult->item(row,0)->text().toInt();
    emit sendDa(i);
    coladdlook.exec();
    if(ui->clookmentw->rowCount()==(j+1))COLshowlook();}
}

void COLwidget::on_deleteclook_clicked()
{
    if(ui->consult->currentRow()>-1)
    {
    if(ui->clookmentw->currentRow()>-1){
    QString ID=ui->clookmentw->item(ui->clookmentw->currentRow(),0)->text();
    QSqlQuery query;
    QString strsql=QString("DELETE FROM ReadMen WHERE ReadID='%1'").arg(ID);
    if(query.exec(strsql)) {QMessageBox::information(this,"提示信息","删除成功!");
    COLshowlook();}
    else{QMessageBox::information(this,"提示信息","删除失败!");}
    }else{QMessageBox::information(this, tr("提示信息"), tr("无法删除：未选中删除行！"));}
    }else{QMessageBox::information(this,"提示信息","请选择对应的审批表信息!");}
}

void COLwidget::on_editcreadbtn_clicked()
{
    int j=ui->clookmentw->currentRow();
    int row=ui->consult->currentRow();
    if(row<=-1){QMessageBox::information(this,"提示信息","请选择对应的审批表信息!");
    }else{
        if(j>-1){
        COLeditlookDialog coleditlook(this);
        connect(this,SIGNAL(sendData(QStringList)),&coleditlook,SLOT(receivestr(QStringList)));
        QStringList fan;
        fan<<ui->clookmentw->item(j,0)->text()<<ui->clookmentw->item(j,1)->text()<<ui->clookmentw->item(j,2)->text()<<ui->clookmentw->item(j,3)->text()<<ui->clookmentw->item(j,4)->text();
    emit sendData(fan);
    coleditlook.exec();
    COLshowlook();
    ui->clookmentw->selectRow(j);}
    else{QMessageBox::information(this,"提示信息","请选择要编辑的行!");}
    }
}

void COLwidget::on_caddrecordbtn_clicked()
{
    int i;
    int j=ui->consulttw->rowCount();
    int row=ui->consult->currentRow();
    if(row<=-1){QMessageBox::information(this,"提示信息","请选择对应的审批表之后进行添加记录!");
    }else{
        COLaddrecordDialog coladdre(this);
        connect(this,SIGNAL(sendDa(int)),&coladdre,SLOT(receiveApprovalID(int)));
    i=ui->consult->item(row,0)->text().toInt();
    emit sendDa(i);
    coladdre.exec();
    if(ui->consulttw->rowCount()==(j+1))COLshowrecord();}
}

void COLwidget::on_cdeleterecordbtn_clicked()
{
    if(ui->consult->currentRow()>-1)
    {
    if(ui->consulttw->currentRow()>-1){
        QString ID=ui->consulttw->item(ui->consulttw->currentRow(),0)->text();
        QSqlQuery query;
        QString strsql=QString("DELETE FROM recordall WHERE recordID='%1'").arg(ID);
    if(query.exec(strsql)) {QMessageBox::information(this,"提示信息","删除成功!");
    COLshowrecord();}
    else{QMessageBox::information(this,"提示信息","删除失败!");}
    }else{QMessageBox::information(this, tr("提示信息"), tr("无法删除：未选中删除行！"));}
    }else{QMessageBox::information(this,"提示信息","请选择对应的审批表信息!");}
}

void COLwidget::on_ceditrecordbtn_clicked()
{
    int j=ui->consulttw->currentRow();
    int row=ui->consult->currentRow();
    if(row<=-1){QMessageBox::information(this,"提示信息","请选择对应的审批表信息!");
    }else{
        if(j>-1){
        COLeditrecordDialog coleditre(this);
        connect(this,SIGNAL(sendData(QStringList)),&coleditre,SLOT(receivestr(QStringList)));
        QStringList fan;
        fan<<ui->consulttw->item(j,0)->text()<<ui->consulttw->item(j,1)->text()<<ui->consulttw->item(j,2)->text()<<ui->consulttw->item(j,3)->text()<<ui->consulttw->item(j,4)->text()<<ui->consulttw->item(j,5)->text()<<ui->consulttw->item(j,6)->text()<<ui->consulttw->item(j,7)->text()<<ui->consulttw->item(j,8)->text()<<ui->consulttw->item(j,9)->text();
        emit sendData(fan);
    coleditre.exec();
    COLshowrecord();
    ui->consulttw->selectRow(j);}
    else{QMessageBox::information(this,"提示信息","请选择要编辑的行!");}
    }
}

void COLwidget::on_cupbtn_clicked()
{
    int m_CurrentRow = ui->consulttw->currentRow();//获取当前行的Index
    if (m_CurrentRow != -1)//如果选中了数据，（未选择数据之前，返回值是-1）
    {
        if (m_CurrentRow != 0)//如果选定行不在第一行
        {
            COLSwapTwoRow(m_CurrentRow, m_CurrentRow - 1);
            ui->consulttw->setCurrentCell(m_CurrentRow - 1, QItemSelectionModel::Select);//移动过后继续选定该行
        }
    }
    else {
        if (ui->consulttw->rowCount() != 0)//如果有数据，但是currentRow=--1 说明没有选择数据,把焦点定位到第一行
        {
            QMessageBox::critical(this, tr("出现错误"), tr("无法移动：未选中数据"));
        }
        else
        {
            QMessageBox::critical(this, tr("出现错误"), tr("无法移动：表格没有数据"));
        }
    }
    for(int i=0;i<ui->consulttw->rowCount();i++){
    if(QString::compare((ui->consult->item(ui->consult->currentRow(),3)->text()),ui->consulttw->item(i,2)->text())<0){
        ui->consulttw->item(i,2)->setForeground(QBrush(QColor(255,0,0)));
    }}
}

void COLwidget::on_cdownbtn_clicked()
{
    int m_CurrentRow = ui->consulttw->currentRow();
    if (m_CurrentRow != -1)//如果选中了一行
    {
        if (m_CurrentRow != (ui->consulttw->rowCount() - 1))//如果不是最后一行
        {
            COLSwapTwoRow(m_CurrentRow, m_CurrentRow + 1);
            ui->consulttw->setCurrentCell(m_CurrentRow + 1, QItemSelectionModel::Select);//移动过后继续选定该行
        }

    }
    else {
        if (ui->consulttw->rowCount() != 0)//如果有数据，但是currentRow=--1 说明没有选择数据,把焦点定位到第一行
        {
            QMessageBox::critical(this, tr("出现错误"), tr("无法移动：未选中数据"));
        }
        else
        {
            QMessageBox::critical(this, tr("出现错误"), tr("无法移动：表格没有数据"));
        }
    }
    for(int i=0;i<ui->consulttw->rowCount();i++){
    if(QString::compare((ui->consult->item(ui->consult->currentRow(),3)->text()),ui->consulttw->item(i,2)->text())<0){
        ui->consulttw->item(i,2)->setForeground(QBrush(QColor(255,0,0)));
    }}
}

void COLwidget::COLSwapTwoRow(int selectRow, int targetRow)
{
    QStringList selectRowLine, targetRowLine;//创建两个QStringList用于储存表格数据
    int rowCount=10;//十列
    for (int i = 0; i < rowCount; i++)
    {
        selectRowLine.append(ui->consulttw->item(selectRow, i)->text());//获取文本，并添加进QStringLine
        targetRowLine.append(ui->consulttw->item(targetRow, i)->text());
    }
    for (int i = 0; i < rowCount; i++)
    {
        ui->consulttw->setItem(selectRow, i, new QTableWidgetItem(targetRowLine.at(i)));//设置文本
        ui->consulttw->setItem(targetRow, i, new QTableWidgetItem(selectRowLine.at(i)));
    }
}

void COLwidget::on_previewbtn_clicked()
{
    if(ui->consult->currentRow()>-1){
        QString filePth = QCoreApplication::applicationDirPath() + "/";
        QString fileName = QFileDialog::getSaveFileName(this, tr("导出PDF文件"), filePth, "*.pdf");
        if (!fileName.isEmpty())
        {
            // 如果文件后缀为空，则默认使用.pdf
            if (QFileInfo(fileName).suffix().isEmpty())
            {
                fileName.append(".pdf");
            }
            QPrinter *printer = new QPrinter(QPrinter::ScreenResolution);
            // 指定输出格式为pdf
            printer->setPageSize(QPrinter::A4);
            printer->setOutputFormat(QPrinter::PdfFormat);
            printer->setOutputFileName(fileName);
            QTextDocument text_document;
            QString html;
            html=saveHtmlToPDFconsult(fileName);
            text_document.setHtml(html);
            text_document.print(printer);
            text_document.end();
            }}
        else{QMessageBox::information(this,"提示信息","未选择打印条目行!");}
}

QString COLwidget::saveHtmlToPDFconsult(QString Q)
{
    QString html = "";
    html += "<html>";
    html += "<head>";
    html += "<title>"+Q+"</title>";
    html += "<head>";
    html += "<body>";
    html += "<h1>查阅审批信息</h1>";
    html += "<font size=\"4\">";
    html += "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\";>";
        //html += "<thead>";
    html += "<tr>";
        html += "<th colspan=\"4\">审批信息</th>";
    html += "</tr>";
        html += "<tr align=\"center\">";
            html += "<td align=\"center\">阅档方式</td>";
            html += "<td align=\"center\">"+ui->consult->item(ui->consult->currentRow(),1)->text()+"</td>";
            html += "<td align=\"center\">借阅查阅</td>";
            if(ui->lendrbtn->isChecked()){html += "<td align=\"center\">借阅</td>";}
            else{html += "<td align=\"center\">查阅</td>";}
        html += "</tr>";

        html += "<tr>";
            html += "<td align=\"center\">阅档时间</td>";
            html += "<td align=\"center\">"+ui->consult->item(ui->consult->currentRow(),2)->text()+"</td>";
            html += "<td align=\"center\">退还时间</th>";
            html += "<td align=\"center\">"+ui->consult->item(ui->consult->currentRow(),3)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">阅档事由</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->cconsultreason->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">阅档事由</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->cconsultcontent->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">阅档单位审批领导</td>";
            html += "<td align=\"center\">"+ui->cinquerylead->text()+"</td>";
            html += "<td align=\"center\">阅档单位审批时间</td>";
            html += "<td align=\"center\">"+ui->cinquerydate->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">阅档单位领导意见</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->cinqueryopinion->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">管档科室审批领导</td>";
            html += "<td align=\"center\">"+ui->cofficelead->text()+"</td>";
            html += "<td align=\"center\">管档科室审批时间</td>";
            html += "<td align=\"center\">"+ui->cofficedate->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">管档科室领导意见</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->cofficeopinion->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">管档单位审批领导</td>";
            html += "<td align=\"center\">"+ui->cunitlead->text()+"</td>";
            html += "<td align=\"center\">管档单位审批时间</td>";
            html += "<td align=\"center\">"+ui->cunitdate->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">管档单位领导意见</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->cunitopinion->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">备注</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->consult->item(ui->consult->currentRow(),5)->text()+"</td>";
        html += "</tr>";
        html += "</table>";


        html += "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
        html += "<tr>";
            html += "<th colspan=\"5\">查阅对象信息</th>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">查阅对象</td>";
            html += "<td align=\"center\">对象单位</td>";
            html += "<td align=\"center\">对象职务</td>";
            html += "<td align=\"center\">政治面貌</td>";
            html += "<td align=\"center\">卷数</td>";
        html += "</tr>";
    for(int i=0;i<ui->cobjectmentw->rowCount();i++){
        html += "<tr>";
            html += "<td align=\"center\">"+ui->cobjectmentw->item(i,1)->text()+"</td>";
            html += "<td align=\"center\">"+ui->cobjectmentw->item(i,2)->text()+"</td>";
            html += "<td align=\"center\">"+ui->cobjectmentw->item(i,3)->text()+"</td>";
            html += "<td align=\"center\">"+ui->cobjectmentw->item(i,4)->text()+"</td>";
            html += "<td align=\"center\">"+ui->cobjectmentw->item(i,5)->text()+"</td>";
        html += "</tr>";
}
    html += "</table>";
    html += "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
    html += "<tr>";
        html += "<th colspan=\"4\">阅档人信息</th>";
    html += "</tr>";
    html += "<tr>";
        html += "<td align=\"center\">阅档人</td>";
        html += "<td align=\"center\">阅档单位</td>";
        html += "<td align=\"center\">阅档人职务</td>";
        html += "<td align=\"center\">政治面貌</td>";
    html += "</tr>";
for(int i=0;i<ui->clookmentw->rowCount();i++){
    html += "<tr>";
        html += "<td align=\"center\">"+ui->clookmentw->item(i,1)->text()+"</td>";
        html += "<td align=\"center\">"+ui->clookmentw->item(i,2)->text()+"</td>";
        html += "<td align=\"center\">"+ui->clookmentw->item(i,3)->text()+"</td>";
        html += "<td align=\"center\">"+ui->clookmentw->item(i,4)->text()+"</td>";
    html += "</tr>";
}
html += "</table>";
html += "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
html += "<tr>";
    html += "<th>查阅记录</th>";
html += "</tr>";
    for(int i=0;i<ui->consulttw->rowCount();i++){
    html += "<tr>";
        html += "<td>实际阅档时间："+ui->consulttw->item(i,1)->text()+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                +"实际退还时间："+ui->consulttw->item(i,2)->text()+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                +"阅档人："+ui->consulttw->item(i,3)->text()+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                +"阅档单位："+ui->consulttw->item(i,4)->text()+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                +"阅档对象："+ui->consulttw->item(i,5)->text()+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                +"对象单位："+ui->consulttw->item(i,6)->text()+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                +"阅档内容："+ui->consulttw->item(i,7)->text()+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                +"备注："+ui->consulttw->item(i,8)->text()+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                +"</td>";
    html += "</tr>";
    }
html += "</table>";
    html += "</font>";
    html += "</body>";
    html += "</html>";
return html;
}

void COLwidget::COLshowsort(int clounm)
{
    if(identification==0){
    ui->consult->sortItems(clounm,Qt::DescendingOrder);
    identification=1;}
    else{ui->consult->sortItems(clounm,Qt::AscendingOrder);
    identification=0;}
}
